home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / jpl_c.zip / PUTC.C < prev    next >
Text File  |  1986-05-18  |  3KB  |  107 lines

  1. /* 1.2  01-08-86                        (putc.c) 
  2.  ************************************************************************
  3.  *            Robert C. Tausworthe                *
  4.  *            Jet Propulsion Laboratory            *
  5.  *            Pasadena, CA 91009        1986        *
  6.  ************************************************************************/
  7.  
  8. #include "defs.h"
  9. #include "stdtyp.h"
  10. #include "stdio.h"
  11.  
  12. /************************************************************************/
  13.     METACHAR
  14. putc(c, fp)        /* Put character c on FILE fp.  Return c or EOF
  15.                on error condition.                */
  16. /*----------------------------------------------------------------------*/
  17. METACHAR c;
  18. FAST FILE *fp;
  19. {
  20.     METACHAR flush();
  21.  
  22.     _rowcol(c, fp);
  23.     if (fp->_bptr >= fp->_bend)
  24.         return flush(fp, c & 0xff);
  25.  
  26.     return (*fp->_bptr++ = c) & 0xff;
  27. }
  28. /*\p*********************************************************************/
  29.     METACHAR
  30. flush(fp, c)        /* Write any already buffered characters onto
  31.                FILE fp.  If c is EOF, return NULL on success,
  32.                EOF on failure.  Otherwise, if i/o is buffered,
  33.                put c in the buffer, and return c.  If io isn't
  34.                buffered, write the char, return c if ok or EOF
  35.                if write error.                 */
  36. /*----------------------------------------------------------------------*/
  37. METACHAR c;
  38. FAST FILE *fp;
  39. {
  40.     FAST int siz;
  41.     char cc;
  42.  
  43.     cc = c;
  44.     if (fp->_flags & _IOERR)
  45.         return EOF;
  46.  
  47.     if (fp->_flags & _DIRTY)
  48.     {    siz = fp->_bptr - fp->_buff;
  49.         if (write(fp->_unit, fp->_buff, siz) ISNT siz)
  50.         {    fp->_flags |= _IOERR;
  51.             fp->_bend = fp->_bptr = NULL;
  52.             return EOF;
  53.         }
  54.     }
  55.     if (c IS EOF)
  56.     {    fp->_flags &= ~_DIRTY;
  57.         fp->_bend = fp->_bptr = NULL;
  58.         return NULL;
  59.     }
  60.     if (fp->_buff IS NULL)        /* if no buffer yet assigned.    */
  61.         getbuf(fp);
  62.     if (fp->_buflen IS 1)            /* unbuffered I/O    */
  63.     {    if (write(fp->_unit, &cc, 1) ISNT 1)
  64.         {    fp->_flags |= _IOERR;
  65.             fp->_bend = fp->_bptr = NULL;
  66.             return EOF;
  67.         }
  68.         return c & 0xff;
  69.     }
  70.     fp->_bptr = fp->_buff;
  71.     fp->_bend = fp->_buff + fp->_buflen;
  72.     fp->_flags |= _DIRTY;
  73.     return (*fp->_bptr++ = c) & 0xff;
  74. }
  75. /*\p*********************************************************************/
  76.     VOID
  77. fclosall()        /* close all stream files            */
  78.  
  79. /*----------------------------------------------------------------------*/
  80. {
  81.     FILE *fp;
  82.  
  83.     for (fp = IObuffs; fp < IObuffs + MAXSTREAM; fp++)
  84.         if (fp->_flags)
  85.             fclose(fp);
  86.     return;
  87.  
  88. }
  89.  
  90. /*************************************************************************/
  91.     METACHAR
  92. putca(c, fp)        /* Put ASCII character c on FILE fp.  Return c or
  93.                EOF on error condition.  Add a '\r' to the
  94.                output when c = '\n', as file fp is assumed
  95.                to be text.                    */
  96. /*----------------------------------------------------------------------*/
  97. METACHAR c;
  98. FILE *fp;
  99. {
  100. #ifdef CROUTADD
  101.     if (c IS '\n')
  102.         if (putc('\r', fp) IS EOF)
  103.             return EOF;
  104. #endif
  105.     return putc(c, fp);
  106. }
  107.